﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class Edit_sj : System.Web.UI.Page
    {
        public int SortID, TypeID;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }
            if (!IsPostBack)
            {
                EditDataBind();
            }
        }

        protected string GetImageUrl(string imgUrl)
        {
            imgUrl = imgUrl.Replace("..", "~/");
            imgUrl = imgUrl.Replace("\\", "/");
            imgUrl = imgUrl.Trim();
            return imgUrl;
        }

        private void EditDataBind()
        {
            int ID = Convert.ToInt32(Request.QueryString["ID"].ToString());

            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            //定义所需SQL语句
            string sql = "select * from info where info_id=" + ID;

            DataTable dataTable = null;
            string sortId = string.Empty;
            string typeId = string.Empty;

            dataTable = classdb.GetTables(sql);

            if (dataTable != null && dataTable.Rows.Count > 0)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    showname.Text = row["showname"].ToString();           //信息标题
                    Edit_info_content.Text = row["content"].ToString();             //信息内容
                    linkman.Text = row["linkman"].ToString();             //联系人
                    phone.Text = row["phone"].ToString();                 //联系电话
                    company.Text = row["company"].ToString();             //公司名称
                    address.Text = row["address"].ToString();             //公司地址
                    country.Text = row["sf"].ToString();             //所在省份
                    city.Text = row["city"].ToString();                   //所在城市
                    email.Text = row["mail"].ToString();                  //邮件
                    fax.Text = row["fax"].ToString();                     //传真
                    web.Text = row["web"].ToString();                     //传真
                    mobile.Text = row["mobile"].ToString();
                    period.SelectedValue = row["period"].ToString();            //有效期
                    InfoType.SelectedValue = row["type"].ToString();            //信息类型
                    sortId = row["sortid"].ToString();
                    typeId = row["typeid"].ToString();
                }
                //定义所需SQL语句
                string sql3 = "select * from class_1";
                //引入数据库操作类
                BigClass.DataSource = classdb.GetTables(sql3).DefaultView;
                BigClass.DataTextField = "Sort";
                BigClass.DataValueField = "SortID";
                BigClass.DataBind();
                BigClass.SelectedValue = sortId;        //行业大类

                //定义所需SQL语句
                string sql4 = "select * from class_2 where sortid=" + sortId;
                SmallClass.DataSource = classdb.GetTables(sql4).DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();
                SmallClass.SelectedValue = typeId;           //行业小类
            }
        }


        private void BigClass_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //定义所需SQL语句
            string sql = "select * from class_2 where sortid=" + BigClass.SelectedValue;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            DataTable dataTable = null;

            dataTable = classdb.GetTables(sql);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();                //行业小类
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(showname.Text) || string.IsNullOrEmpty(period.SelectedValue))
            {
                Response.Write("<script>alert('请按要求输入');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }
            int ID = Convert.ToInt32(Request.QueryString["ID"]);
            //定义所需SQL
            string sql = "update info set showname='" + showname.Text.Trim() + "',type='" + InfoType.SelectedValue + "',period='" + period.SelectedValue + "',sortid='" + BigClass.SelectedValue + "',typeid='" + SmallClass.SelectedValue + "'";
            sql += ",content='" + Edit_info_content.Text + "',linkman='" + linkman.Text + "',phone='" + phone.Text + "',company='" + company.Text + "'";
            sql += ",address='" + address.Text + "',sf='" + country.Text + "',city='" + city.Text + "',mail='" + email.Text + "',fax='" + fax.Text + "'";
            sql += ",web='" + web.Text + "',mobile='" + mobile.Text + "'";
            sql += " where info_ID=" + ID;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            if (classdb.GetNonResult(sql) != 0)
            {
                Response.Write("<script>alert('操作成功');</script>");
                Response.Write("<script>history.go(-2);</script>");
                Response.End();
            }
            else
            {
                Response.Write("<script>alert('操作失败！请重新操作！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }
        }
    }
}